﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace DownloadMag.Core
{
    /// <summary>
    /// 下载资源位置对象
    /// </summary>
    public class ResourceLocation
    {
        #region Fields

        private string m_URL;
        private string m_Referer;
        private bool m_Authenticate;
        private string m_UserInfo;
        private IProtocolProvider provider;

        #endregion

        #region Constructor
        public ResourceLocation()
        {
        }

        public static ResourceLocation FromURL(string url)
        {
            ResourceLocation rl = new ResourceLocation();
            rl.URL = url;
            return rl;
        }

        public static ResourceLocation[] FromURLArray(string[] urls)
        {
            List<ResourceLocation> result = new List<ResourceLocation>();

            for (int i = 0; i < urls.Length; i++)
            {
                if (IsURL(urls[i]))
                {
                    result.Add(ResourceLocation.FromURL(urls[i]));
                }
            }

            return result.ToArray();
        }

        public static ResourceLocation FromURL(
            string url,
            bool authenticate,
            string user,
            string password)
        {
            ResourceLocation rl = new ResourceLocation();
            rl.URL = url;
            rl.Authenticate = authenticate;
            rl.UserInfo = new UserInfo(user, password).ToString(); ;
            return rl;
        }
        #endregion

        #region Properties

        /// <summary>
        /// 设置/获取 下载地址
        /// </summary>
        public string URL
        {
            get { return m_URL; }
            set
            {
                m_URL = value;
                //GetProtocolProvider();
            }
        }

        public bool Authenticate
        {
            get { return m_Authenticate; }
            set { m_Authenticate = value; }
        }

        /// <summary>
        /// 设置/获取 登录用户信息
        /// </summary>
        public string UserInfo
        {
            get { return m_UserInfo; }
            set { m_UserInfo = value; }
        }

        /// <summary>
        /// 设置/获取 引用页
        /// </summary>
        public string Referer
        {
            get { return m_Referer; }
            set { m_Referer = value; }
        }
        #endregion

        #region Methods

        public IProtocolProvider GetProtocolProvider()
        {
            if (provider == null && !String.IsNullOrEmpty(this.URL))
            {
                provider = ProtocolProviderFactory.GetProvider(this.URL);
            }
            return provider;
        }

        public ResourceLocation Clone()
        {
            return (ResourceLocation)this.MemberwiseClone();
        }

        public override string ToString()
        {
            return this.URL;
        }

        public static bool IsURL(string url)
        {
            Match m = Regex.Match(url, @"(?<Protocol>\w+):\/\/(?<Domain>[\w.]+\/?)\S*");
            if (m.ToString() != string.Empty)
            {
                return true;
            }
            return false;
        }

        #endregion
    }
}
